<template>
  <el-upload action="" :show-file-list="false" :auto-upload="false" :on-change="handleChange">
    <el-button>上传合同</el-button>
  </el-upload>
</template>
<script lang="ts" setup>
  import { reactive } from 'vue'
  import { ElMessage } from 'element-plus'
  import { uploadFile2 } from '@/api/common'
  const rule = reactive({
    type: 'pdf|PDF',
    unit: 'MB',
    size: 20
  })
  const emit = defineEmits(['submit'])
  const handleChange = (file: any) => {
    const fileName = file.name.split('.')
    if (!rule.type.includes(fileName.pop())) {
      ElMessage.error('上传文件类型不正确, 请重新上传!')
      return
    }
    if (rule.unit === 'MB') {
      if (file.size / 1024 / 1024 > rule.size) {
        ElMessage.error('文件大小不能超过' + rule.size + 'MB')
        return
      }
    }
    uploadFile2({
      file: file.raw,
      fileName: file.name
    }).then((res: any) => {
      emit('submit', res)
    })
  }
</script>
<style lang="scss" scoped></style>
